package com.zatech.cgnci.project.job;


import com.zatech.cgnci.project.entity.CgnciBusinessInsuranceDO;
import com.zatech.cgnci.project.entity.CgnciReInsuranceCompanyDO;
import com.zatech.cgnci.project.entity.CgnciReinsurancePremiumBillDO;
import com.zatech.cgnci.project.mapper.CgnciBusinessInsuranceMapper;
import com.zatech.cgnci.project.mapper.CgnciReInsuranceCompanyMapper;
import com.zatech.cgnci.project.mapper.CgnciReinsurancePremiumBillMapper;
import com.zatech.cgnci.project.service.CgnciMessageSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 类 CgnciSystemMessagesJob 的实现描述：系统消息通知定时任务类
 * </p>
 *
 * @author za-xiaojingjie
 * @version 2025/2/19 9:55
 * @since 2025/2/19 9:55
 */
@Component
@Slf4j
public class CgnciSystemMessagesJob {
    @Autowired
    private CgnciBusinessInsuranceMapper businessInsuranceMapper;
    @Autowired
    private CgnciReInsuranceCompanyMapper cgnciReInsuranceCompanyMapper;
    @Autowired
    private CgnciReinsurancePremiumBillMapper billMapper;
    @Autowired
    private CgnciMessageSendService cgnciMessageSendService;


    // 每天00:10:00
    @Scheduled(cron = "0 30 08 * * ?")
    public void sendMessage() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        log.info("系统消息定时任务开始执行：at " + formatter.format(date));
        try {
            checkPolicyOrContractExpire();
            checkBillExpire();
            checkRatingUpdateExpire();
        } catch (Exception e) {
            log.error("系统消息定时任务执行异常： " + e.getMessage());
        }
    }

    private void checkPolicyOrContractExpire() {
        List<CgnciBusinessInsuranceDO> businessInsurancesList = businessInsuranceMapper.findExpireBusinessInsurance();
        for (CgnciBusinessInsuranceDO businessInsurance : businessInsurancesList) {
            String userCode = getUserCodeByUserName(businessInsurance.getCreateUser());
            if ("1".equals(businessInsurance.getBusinessType())) {
                cgnciMessageSendService.sendPolicyExpireMessage(businessInsurance.getBusinessNo(), userCode);
            } else {
                cgnciMessageSendService.sendContractExpireMessage(businessInsurance.getBusinessNo(), businessInsurance.getProjectName(), userCode);
            }
        }
    }

    private void checkBillExpire() {
        List<CgnciReinsurancePremiumBillDO> needSendMessagesBill = billMapper.getNeedSendMessagesBill();
        for (CgnciReinsurancePremiumBillDO billDO : needSendMessagesBill) {
            String userCode = getUserCodeByUserName(billDO.getCreateUser());
            cgnciMessageSendService.sendBillExpireMessage(billDO.getInvoiceNumber(), userCode);
        }
    }

    private void checkRatingUpdateExpire() {

        List<CgnciReInsuranceCompanyDO> firstReminderReinsurer = cgnciReInsuranceCompanyMapper.getFirstReminderReinsurer();
        List<CgnciReInsuranceCompanyDO> secondReminderReinsurer = cgnciReInsuranceCompanyMapper.getSecondReminderReinsurer();
        //首次提醒
        firstReminderReinsurer.forEach(item -> {
            cgnciMessageSendService.sendRatingUpdateMessage(item.getCompanyNameCn(), getUserCodeByUserName(item.getCreateUser()));
        });
        secondReminderReinsurer.forEach(item -> {
            if (item.getUpdateUser() != null) {
                String userCode = getUserCodeByUserName(item.getUpdateUser());
                cgnciMessageSendService.sendRatingUpdateMessage(item.getCompanyNameCn(), userCode);
            } else {
                String userCode = getUserCodeByUserName(item.getCreateUser());
                cgnciMessageSendService.sendRatingUpdateMessage(item.getCompanyNameCn(), userCode);
            }
        });
    }

    private String getUserCodeByUserName(String userName) {
        String[] split = userName.split("-");
        if (split.length < 2) {
            throw new RuntimeException("创建人信息格式错误");
        }
        return split[1];
    }
}
